package com.qjq.glasses_shopping.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.pojo.Goods;
import com.qjq.glasses_shopping.pojo.GoodsDesc;
import com.qjq.glasses_shopping.pojo.GoodsSearchParam;
import com.qjq.glasses_shopping.result.BaseResult;
import com.qjq.glasses_shopping.service.GoodsService;
import com.qjq.glasses_shopping.service.SearchService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户商品搜索控制器
 */

/**
 * 用户商品搜索
 */
@RestController
@RequestMapping("/user/goodsSearch")
public class GoodsSearchController {
    @Resource
    private SearchService searchService;
    @Resource
    private GoodsService goodsService;


    @GetMapping("/autoSuggest")
    public BaseResult<List<GoodsDesc>> autoSuggest(@RequestParam("keyword") String keyword) {
        List<GoodsDesc> autosuggest = searchService.autosuggest(keyword);
        return BaseResult.ok(autosuggest);
    }
    /**
     * 搜索商品
     * @param param 搜索条件
     * @return 搜索结果
     */
    @PostMapping("/search")
    public BaseResult<Page<Goods>> search( @RequestBody GoodsSearchParam param){
        String goodsName = param.getGoodsName();
        Integer page = param.getPage();
        Integer size = param.getSize();
        page = page == null? 1 : page;
        size = size == null? 10 : size;
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        Page<Goods> result = searchService.search(goods, page, size);
        return BaseResult.ok(result);
    }

    /**
     * 根据商品id查询商品详情
     * @param id 商品id
     * @return 商品详情
     */
    @GetMapping("/findDesc")
    public BaseResult<GoodsDesc> findDesc(Long id){
        GoodsDesc goodsDesc = goodsService.findDesc(id);
        return BaseResult.ok(goodsDesc);
    }
}
